package com.example.administrator.pregnancyassistence.activity;

import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.example.administrator.pregnancyassistence.R;
import com.example.administrator.pregnancyassistence.fragment.BabyFragment;
import com.example.administrator.pregnancyassistence.fragment.NewsListFragment;
import com.example.administrator.pregnancyassistence.fragment.PersonalFragment;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private Map<String, ImageView> imageViewMap = new HashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        //使用ToolBar替换ActionBar
//        Toolbar toolbar= findViewById(R.id.toolbar);
//        setSupportActionBar(toolbar);

        //获取FragmentTabHost对象
        FragmentTabHost fragmentTabHost = findViewById(android.R.id.tabhost);
        //初始化FragmentTabHost
        fragmentTabHost.setup(this,
                getSupportFragmentManager(),//FragmentManager对象用来管理多个Fragment
                android.R.id.tabcontent);//真正显示内容页面的容器的id
        //创建tabspec的对象    
        
        //Baby
        TabHost.TabSpec baby= fragmentTabHost.newTabSpec("baby")
                .setIndicator(getTabSpecView("baby",R.mipmap.baby,"首页"));
        fragmentTabHost.addTab(baby,
                BabyFragment.class,
                null);

        //News
        TabHost.TabSpec news= fragmentTabHost.newTabSpec("news")
                .setIndicator(getTabSpecView("news",R.mipmap.news1,"资讯"));
        fragmentTabHost.addTab(news,
                NewsListFragment.class,
                null);

//        //Make Friends
//        TabHost.TabSpec mf= fragmentTabHost.newTabSpec("mf")
//                .setIndicator(getTabSpecView("mf",R.mipmap.mf1,"她她圈"));
//        fragmentTabHost.addTab(mf,
//                MakefriendsFragment.class,
//                null);

        //Personal
        TabHost.TabSpec personal= fragmentTabHost.newTabSpec("personal")
                .setIndicator(getTabSpecView("personal",R.mipmap.personal1,"我的"));
        fragmentTabHost.addTab(personal,
                PersonalFragment.class,
                null);


        //默认选中第一项
        fragmentTabHost.setCurrentTab(0);
        imageViewMap.get("baby").setImageResource(R.mipmap.baby);

        //切换选项卡的事件监听器
        fragmentTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                switch (tabId){
                    case "baby":
                        imageViewMap.get("news").setImageResource(R.mipmap.news1);
                        imageViewMap.get("baby").setImageResource(R.mipmap.baby);
//                        imageViewMap.get("mf").setImageResource(R.mipmap.mf1);
                        imageViewMap.get("personal").setImageResource(R.mipmap.personal1);
                        break;
                    case "news":
                        imageViewMap.get("news").setImageResource(R.mipmap.news);
                        imageViewMap.get("baby").setImageResource(R.mipmap.baby1);
//                        imageViewMap.get("mf").setImageResource(R.mipmap.mf1);
                        imageViewMap.get("personal").setImageResource(R.mipmap.personal1);
                        break;
//                    case "mf":
//                        imageViewMap.get("news").setImageResource(R.mipmap.news1);
//                        imageViewMap.get("baby").setImageResource(R.mipmap.baby1);
//                        imageViewMap.get("mf").setImageResource(R.mipmap.mf);
//                        imageViewMap.get("personal").setImageResource(R.mipmap.personal1);
//                        break;
                    case "personal":
                        imageViewMap.get("news").setImageResource(R.mipmap.news1);
                        imageViewMap.get("baby").setImageResource(R.mipmap.baby1);
//                        imageViewMap.get("mf").setImageResource(R.mipmap.mf1);
                        imageViewMap.get("personal").setImageResource(R.mipmap.personal);
                        break;
                }
            }
        });
    }

    private View getTabSpecView(String tag, int imageResId, String title) {
        LayoutInflater layoutInflater = getLayoutInflater();
        View view = layoutInflater.inflate(R.layout.tabspec_layout,null);

        //获取控件对象
        ImageView imageView = view.findViewById(R.id.iv_icon);
        imageView.setImageResource(imageResId);

        TextView textView = view.findViewById(R.id.tv_icon);
        textView.setText(title);

        imageViewMap.put(tag,imageView);
        return view;
    }
}
